const express = require('express')
const app = express()
const port = 3000

const writeList = ['localhost', '127.0.0.1']  // 可以配置网址 ip
const preventHotLingking = (req, res, next) => {
    const referer = req.get('referer')  // referer如果是直接打开的资源是获取不到的  需要发送请求
    console.log(referer)
    if (referer) {
        const { hostname } = new URL(referer)
        console.log('hostname', hostname, writeList.includes(hostname))
        if (!writeList.includes(hostname)) {
            res.status(403).json('not fount')
        }

    }
    next()
}
app.use(preventHotLingking)

// 初始化静态资源目录
app.use('/assets', express.static('static'))
app.get('/', (req, res) => {
    res.send('Hello World!')
})


app.listen(port, () => console.log(`Example app listening on port ${port}!`))